<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>5-4 元素居中：position</title>
  <style>
    .parent {
      width: 200px;
      height: 200px;
      background: #ccc;
      /* 父相子绝 */
      position: relative;
    }

    .child {
      width: 100px;
      height: 100px;
      background: #e00;
      /* 开启定位 */
      position: absolute;
      left: 50%;
      top: 50%;
      /* 方法一 margin负值 -兼容性较好 */
      /* margin-left: -50px;
      margin-top: -50px; */

      /* 方法二 css3属性 transfrom ie9+ */
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <div class="parent">parent...
    <div class="child">child...</div>
  </div>
</body>

</html>